#include "NewParameters.h"

#include <QFile>
#include <QTextStream>

#include "Clock.h"
#include <iostream>


NewParameters::NewParameters()
{
 
}

NewParameters::~NewParameters()
{

}


void NewParameters::getParameters(QString fileName)
{
  QTextStream cout(stdout);
  QDomDocument doc;
  //doc.createElement("Parameters");
  QFile file(fileName);
  if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    cout<<"error opening the file"<<endl;
  if(!doc.setContent(&file))
    {
      file.close();
    }
  file.close();
  sample.clear();
  QDomElement root;
  root=doc.documentElement();
  if(root.tagName()!="Parameters")
    cout<<"file is not good"<<endl;
  QDomNode firstNode=root.firstChild();
  while(!firstNode.isNull())
    {
      QDomElement firstElement=firstNode.toElement();
      if(!firstElement.isNull())
	{
	  if(firstElement.tagName()=="Time")
	    {
	      time["window"]=firstElement.attribute("timeWindow","").toFloat();
	      time["step"]=firstElement.attribute("timeStep","").toFloat();
	      time["maxStep"]=firstElement.attribute("maxTimeStep","").toFloat();
	    }
	  if(firstElement.tagName()=="Current")
	    {
	      current["value"]=firstElement.attribute("value","").toFloat();
	      current["max"]=firstElement.attribute("max","").toFloat();
	      current["min"]=firstElement.attribute("min","").toFloat();
	      current["step"]=firstElement.attribute("step","").toFloat();
	    }
	  if(firstElement.tagName()=="Field")
	    {
	      QDomNode secondNode=firstNode.firstChild();
	      while(!secondNode.isNull())
		{
		  QDomElement secondElement=secondNode.toElement();
		  if(!secondElement.isNull())
		    {
		      if(secondElement.tagName()=="values")
			{
			  field["value"]=secondElement.attribute("value","").toFloat();
			  field["max"]=secondElement.attribute("max","").toFloat();
			  field["min"]=secondElement.attribute("min","").toFloat();
			  field["step"]=secondElement.attribute("step","").toFloat();
			}
		      if(secondElement.tagName()=="theta")
			{
			  field["theta"]=secondElement.attribute("value","").toFloat();
			  field["thetaMin"]=secondElement.attribute("min","").toFloat();
			  field["thetaMax"]=secondElement.attribute("max","").toFloat();
			  field["thetaStep"]=secondElement.attribute("step","").toFloat();
			}
		      if(secondElement.tagName()=="phi")
			{
			  field["phi"]=secondElement.attribute("value","").toFloat();
			  field["phiMin"]=secondElement.attribute("min","").toFloat();
			  field["phiMax"]=secondElement.attribute("max","").toFloat();
			  field["phiStep"]=secondElement.attribute("step","").toFloat();
			}
		      secondNode=secondNode.nextSibling();
		    }
		}
	    }
	  if(firstElement.tagName()=="CurrentPulse")
	    {
	      QDomNode secondNode=firstNode.firstChild();
	      while(!secondNode.isNull())
		{
		  QDomElement secondElement=secondNode.toElement();
		  if(!secondElement.isNull())
		    {
		      if(secondElement.tagName()=="pulse")
			{
			  currentPulseNumber=secondElement.attribute("numberOfPulses","").toInt();
			  currentPulseType=secondElement.attribute("type","");
			}
		      if(secondElement.tagName()=="length")
			{
			  currentPulse["length"]=secondElement.attribute("value","").toFloat();
			  currentPulse["min"]=secondElement.attribute("min","").toFloat();
			  currentPulse["max"]=secondElement.attribute("max","").toFloat();
			  currentPulse["step"]=secondElement.attribute("step","").toFloat();
			}
		      secondNode=secondNode.nextSibling();
		    }
		}
	    }

	  if(firstElement.tagName()=="FieldPulse")
	    {
	      QDomNode secondNode=firstNode.firstChild();
	      while(!secondNode.isNull())
		{
		  QDomElement secondElement=secondNode.toElement();
		  if(!secondElement.isNull())
		    {
		      if(secondElement.tagName()=="pulse")
			{
			  fieldPulseNumber=secondElement.attribute("numberOfPulses","").toInt();
			  fieldPulseType=secondElement.attribute("type","");
			  fieldPulse["min"]=secondElement.attribute("min","").toFloat();
			  fieldPulse["max"]=secondElement.attribute("max","").toFloat();
			  fieldPulse["step"]=secondElement.attribute("step","").toFloat();
			}
		      if(secondElement.tagName()=="length")
			{
			  fieldPulse["length"]=secondElement.attribute("value","").toFloat();
			  fieldPulse["min"]=secondElement.attribute("min","").toFloat();
			  fieldPulse["max"]=secondElement.attribute("max","").toFloat();
			  fieldPulse["step"]=secondElement.attribute("step","").toFloat();
			}
		      secondNode=secondNode.nextSibling();
		    }
		}
	    }
	  if(firstElement.tagName()=="DoublePulse")
	    {
	      QDomNode secondNode=firstNode.firstChild();
	      while(!secondNode.isNull())
		{
		  QDomElement secondElement=secondNode.toElement();
		  if(!secondElement.isNull())
		    {
		      if(secondElement.tagName()=="first")
			{
			  doublePulse["firstLength"]=secondElement.attribute("length","").toFloat();
			  doublePulse["firstAmplitude"]=secondElement.attribute("amplitude","").toFloat();
			}
		      if(secondElement.tagName()=="second")
			{
			  doublePulse["secondLength"]=secondElement.attribute("length","").toFloat();
			  doublePulse["secondAmplitude"]=secondElement.attribute("amplitude","").toFloat();
			  doublePulse["secondMin"]=secondElement.attribute("min","").toFloat();
			  doublePulse["secondMax"]=secondElement.attribute("max","").toFloat();
			  doublePulse["secondStep"]=secondElement.attribute("step","").toFloat();
			}
		      if(secondElement.tagName()=="delay")
			{
			  doublePulse["delay"]=secondElement.attribute("value","").toFloat();
			  doublePulse["delayMin"]=secondElement.attribute("min","").toFloat();
			  doublePulse["delayMax"]=secondElement.attribute("max","").toFloat();
			  doublePulse["delayStep"]=secondElement.attribute("step","").toFloat();
			}
		      secondNode=secondNode.nextSibling();
		    }
		}
	    }
	  if(firstElement.tagName()=="Temperature")
	    {
	      temperature=firstElement.attribute("value","").toInt();
	      nrun=firstElement.attribute("nrun","").toInt();
	    }
	  if(firstElement.tagName()=="Torque")
	    {
	      torque["relAccuracy"]=firstElement.attribute("relAccuracy","").toFloat();
	      torque["limit"]=firstElement.attribute("limTorque","").toFloat();
	      torque["tolerance"]=firstElement.attribute("tolerance","").toFloat();
	    }
	  if(firstElement.tagName()=="polarization")
	    {
	      polarizationType=firstElement.attribute("type","");
	      polarization["qplus"]=firstElement.attribute("qplus","").toFloat();
	      polarization["qminus"]=firstElement.attribute("qminus","").toFloat();
	      polarization["b0"]=firstElement.attribute("b0","").toFloat();
	      polarization["b1"]=firstElement.attribute("b1","").toFloat();
	    }
	  if(firstElement.tagName()=="Sample")
	    {
	      QDomNode secondNode=firstNode.firstChild();
	      while(!secondNode.isNull())
		{
		  QDomElement secondElement=secondNode.toElement();
		  QDomNode thirdNode=secondNode.firstChild();
		  while(!thirdNode.isNull())
		    {
		      QDomElement thirdElement=thirdNode.toElement();
		      if(thirdElement.tagName()=="material")
			{
			  layer["ms"]=thirdElement.attribute("ms","").toFloat();
			  layer["alpha"]=thirdElement.attribute("alpha","").toFloat();
			  layer["polarization"]=thirdElement.attribute("polarization","").toFloat();
			}
		      if(thirdElement.tagName()=="anisotropy")
			{
			  layer["k"]=thirdElement.attribute("k","").toFloat();
			  layer["aniPhi"]=thirdElement.attribute("phi","").toFloat();
			  layer["aniTheta"]=thirdElement.attribute("theta","").toFloat();
			}
		      if(thirdElement.tagName()=="geometry")
			{
			  layer["thickness"]=thirdElement.attribute("thickness","").toFloat();
			  layer["surface"]=thirdElement.attribute("surface").toFloat();
			}
		      if(thirdElement.tagName()=="exchange")
			{
			  layer["upExchange"]=thirdElement.attribute("up","").toFloat();
			  layer["downExchange"]=thirdElement.attribute("down","").toFloat();
			}
		      if(thirdElement.tagName()=="layer")
			{
			  layer["frozen"]=thirdElement.attribute("frozen","").toFloat();
			  layerInt["number"]=thirdElement.attribute("number","").toInt();
			}
		      if(thirdElement.tagName()=="magnetization")
			{
			  QDomNode fourthNode=thirdNode.firstChild();
			  while(!fourthNode.isNull())
			    {
			      QDomElement fourthElement=fourthNode.toElement();
			      if(fourthElement.tagName()=="values")
				{
				  layer["mx"]=fourthElement.attribute("x","").toFloat();
				  layer["my"]=fourthElement.attribute("y","").toFloat();
				  layer["mz"]=fourthElement.attribute("z","").toFloat();
				}
			      if(fourthElement.tagName()=="angle")
				{
				  layer["initialAngle"]=fourthElement.attribute("initial","").toFloat();
				  layer["finalAngle"]=fourthElement.attribute("final","").toFloat();
				  layer["angleStep"]=fourthElement.attribute("step","").toFloat();
				}
			      fourthNode=fourthNode.nextSibling();
			    }
			}
		      if(thirdElement.tagName()=="demagnetizationTensor")
			{
			  QDomNode fourthNode=thirdNode.firstChild();
			  while(!fourthNode.isNull())
			    {
			      QDomElement fourthElement=fourthNode.toElement();
			      if(fourthElement.tagName()=="self")
				{
				  layer["ndx"]=fourthElement.attribute("x","").toFloat();
				  layer["ndy"]=fourthElement.attribute("y","").toFloat();
				  layer["ndz"]=fourthElement.attribute("z","").toFloat();
				}
			      if(fourthElement.tagName()=="mutual")
				{
				  layer["nrx"]=fourthElement.attribute("x","").toFloat();
				  layer["nry"]=fourthElement.attribute("y","").toFloat();
				  layer["nrz"]=fourthElement.attribute("z","").toFloat();
				}
			      fourthNode=fourthNode.nextSibling();
			    }
			}
		      thirdNode=thirdNode.nextSibling();
		    }
		  sample.push_back(layer);
		  secondNode=secondNode.nextSibling();
		}
	    }
	}
      firstNode=firstNode.nextSibling();
    }
}


void NewParameters::setMs(int layer)
{

}

QMap<QString,double> NewParameters::getTime()
{
  return time;
}

void NewParameters::setTime(QMap<QString,double> value)
{
  time=value;
}

QString NewParameters::getCurrentPulseType()
{
  return currentPulseType;
}

QString NewParameters::getFieldPulseType()
{
  return fieldPulseType;
}

QMap<QString,double> NewParameters::getField()
{
  return field;
}

void NewParameters::setField(QMap<QString,double> value)
{
  field=value;
}

QMap<QString,double> NewParameters::getCurrent()
{
  return current;
}

void NewParameters::setCurrent(QMap<QString,double> value)
{
  current=value;
}

QMap<QString,double> NewParameters::getFieldPulse()
{
  return fieldPulse;
}
QMap<QString,double> NewParameters::getCurrentPulse()
{
  return currentPulse;
}

QMap<QString,double> NewParameters::getDoublePulse()
{
  return doublePulse;
}

QVector<QMap<QString,double> > NewParameters::getSample()
{
  return sample;
}

void NewParameters::setSample(QVector<QMap<QString,double> > newSample)
{
  sample = newSample;
}

double NewParameters::getTemperature()
{
  return temperature;
}

void NewParameters::setTemperature(double value)
{
  temperature = value;
}
QMap<QString,double> NewParameters::getPolarization()
{
  return polarization;
}

QString NewParameters::getPolarizationType()
{
  return polarizationType;
}

QMap<QString,double> NewParameters::getTorque()
{
  return torque;
}

void NewParameters::setTorque(QMap<QString,double> value)
{
  torque=value;
}

int NewParameters::getNrun()
{
  return nrun;
}

void NewParameters::setNbRun(int value)
{
  nrun = value;
}

void NewParameters::setCurrentPulse(QMap<QString,double> value)
{
  currentPulse = value;
}

void NewParameters::setDoublePulse(QMap<QString,double> value)
{
  doublePulse = value;
}

void NewParameters::setCurrentPulseType(QString value)
{
  currentPulseType = value;
}

void NewParameters::setPolarizationType(QString value)
{
  polarizationType = value;
}

void NewParameters::setPolarization(QMap<QString,double> value)
{
  polarization = value;
}

void NewParameters::saveParameters(QString fileName)
{
  QDomDocument doc;
  //QFile file(fileName);
  //if(file.open(QIODevice::ReadOnly | QIODevice::Text))
  //cout<<"error opening the file"<<endl;
  /*if(!doc.setContent(&file))
    {
      file.close();
      }*/
  
  QDomElement root,first_Time,first_Current;
  root=doc.createElement("Parameters");
  doc.appendChild(root);
  first_Time=doc.createElement("Time"); 
  first_Time.setAttribute("timeWindow",time["window"]);
  first_Time.setAttribute("timeStep",time["step"]);
  first_Time.setAttribute("maxTimeStep",time["maxStep"]); 
  root.appendChild(first_Time);
  first_Current=doc.createElement("Current");
  first_Current.setAttribute("value",current["value"]);
  first_Current.setAttribute("max",current["max"]);
  first_Current.setAttribute("min",current["min"]);
  first_Current.setAttribute("step",current["step"]);
  root.appendChild(first_Current);
  QDomElement first_Field = doc.createElement("Field");
  QDomElement second_values = doc.createElement("values");
  second_values.setAttribute("value",field["value"]);
  second_values.setAttribute("max",field["max"]);
  second_values.setAttribute("min",field["min"]);
  second_values.setAttribute("step",field["step"]);
  first_Field.appendChild(second_values);
  QDomElement second_theta = doc.createElement("theta");
  second_theta.setAttribute("value",field["theta"]);
  second_theta.setAttribute("max",field["thetaMax"]);
  second_theta.setAttribute("min",field["thetaMin"]);
  second_theta.setAttribute("step",field["thetaStep"]);
  first_Field.appendChild(second_theta);
  QDomElement second_phi = doc.createElement("phi");
  second_phi.setAttribute("value",field["phi"]);
  second_phi.setAttribute("max",field["phiMax"]);
  second_phi.setAttribute("min",field["phiMin"]);
  second_phi.setAttribute("step",field["phiStep"]);
  first_Field.appendChild(second_phi);
  root.appendChild(first_Field);  
  QDomElement first_currentPulse = doc.createElement("CurrentPulse");
  root.appendChild(first_currentPulse);
  QDomElement second_IPulse = doc.createElement("pulse");
  second_IPulse.setAttribute("numberOfPulses",currentPulseNumber);
  second_IPulse.setAttribute("type",currentPulseType);
  first_currentPulse.appendChild(second_IPulse);
  QDomElement second_ILength = doc.createElement("length");
  second_ILength.setAttribute("value",currentPulse["length"]);
  second_ILength.setAttribute("max",currentPulse["max"]);
  second_ILength.setAttribute("min",currentPulse["min"]);
  second_ILength.setAttribute("step",currentPulse["step"]);
  first_currentPulse.appendChild(second_ILength);
  QDomElement first_FieldPulse = doc.createElement("FieldPulse");
  root.appendChild(first_FieldPulse);
  QDomElement second_BPulse = doc.createElement("pulse");
  second_BPulse.setAttribute("numberOfPulses",fieldPulseNumber);
  second_BPulse.setAttribute("type",fieldPulseType);
  first_FieldPulse.appendChild(second_BPulse);
  QDomElement second_BLength = doc.createElement("length");
  second_BLength.setAttribute("value",fieldPulse["length"]);
  second_BLength.setAttribute("max",fieldPulse["max"]);
  second_BLength.setAttribute("min",fieldPulse["min"]);
  second_BLength.setAttribute("step",fieldPulse["step"]);
  first_FieldPulse.appendChild(second_BLength);
  QDomElement first_DoublePulse = doc.createElement("DoublePulse");
  root.appendChild(first_DoublePulse);
  QDomElement second_firstPulse = doc.createElement("first");
  second_firstPulse.setAttribute("length",doublePulse["firstLength"]);
  second_firstPulse.setAttribute("amplitude",doublePulse["firstAmplitude"]);
  first_DoublePulse.appendChild(second_firstPulse);
  QDomElement second_secondPulse = doc.createElement("second");
  second_secondPulse.setAttribute("length",doublePulse["secondLength"]);
  second_secondPulse.setAttribute("amplitude",doublePulse["secondAmplitude"]);
  second_secondPulse.setAttribute("max",doublePulse["secondMax"]);
  second_secondPulse.setAttribute("min",doublePulse["secondMin"]);
  second_secondPulse.setAttribute("step",doublePulse["secondStep"]);
  first_DoublePulse.appendChild(second_secondPulse);
  QDomElement second_Delay = doc.createElement("delay");
  second_Delay.setAttribute("value",doublePulse["delay"]);
  second_Delay.setAttribute("max",doublePulse["delayMax"]);
  second_Delay.setAttribute("min",doublePulse["delayMin"]);
  second_Delay.setAttribute("step",doublePulse["delayStep"]);
  first_DoublePulse.appendChild(second_Delay);
  QDomElement first_Temp = doc.createElement("Temperature");
  root.appendChild(first_Temp);
  first_Temp.setAttribute("value",temperature);
  first_Temp.setAttribute("nrun",nrun);
  QDomElement first_Torque = doc.createElement("Torque");
  root.appendChild(first_Torque);
  first_Torque.setAttribute("relAccuracy",torque["relAccuracy"]);
  first_Torque.setAttribute("limTorque",torque["limit"]);
  first_Torque.setAttribute("tolerance",torque["tolerance"]);
  QDomElement first_pol = doc.createElement("polarization");
  root.appendChild(first_pol);
  first_pol.setAttribute("type",polarizationType);	 
  first_pol.setAttribute("qplus",polarization["qplus"]);
  first_pol.setAttribute("qminus",polarization["qminus"]);
  first_pol.setAttribute("b0",polarization["b0"]);
  first_pol.setAttribute("b1",polarization["b1"]);	
  QDomElement first_Sample = doc.createElement("Sample");
  root.appendChild(first_Sample);
  for(int counter=0;counter<sample.size();counter++)
    {
      QString layerName = "Layer"+QString::number(counter);
      layer=sample[counter];
      QDomElement second_Layer = doc.createElement(layerName); 
      first_Sample.appendChild(second_Layer);
      QDomElement third_Material = doc.createElement("material");
      third_Material.setAttribute("ms",layer["ms"]);
      third_Material.setAttribute("alpha",layer["alpha"]);
      third_Material.setAttribute("polarization",layer["polarization"]);
      second_Layer.appendChild(third_Material);
      QDomElement third_Anisotropy = doc.createElement("anisotropy");
      third_Anisotropy.setAttribute("k",layer["k"]);
      third_Anisotropy.setAttribute("theta",layer["aniTheta"]);
      third_Anisotropy.setAttribute("phi",layer["aniPhi"]);
      second_Layer.appendChild(third_Anisotropy);
      QDomElement third_Geometry = doc.createElement("geometry");
      third_Geometry.setAttribute("thickness",layer["thickness"]);
      third_Geometry.setAttribute("surface",layer["surface"]);
      second_Layer.appendChild(third_Geometry);
      QDomElement third_Exchange = doc.createElement("exchange");
      third_Exchange.setAttribute("up",layer["upExchange"]);
      third_Exchange.setAttribute("down",layer["downExchange"]);
      second_Layer.appendChild(third_Exchange);
      QDomElement third_Layer = doc.createElement("layer");
      third_Layer.setAttribute("frozen",layer["frozen"]);
      third_Layer.setAttribute("number",layerInt["number"]);
      second_Layer.appendChild(third_Layer);
      QDomElement third_mag = doc.createElement("magnetization");
      second_Layer.appendChild(third_mag);
      QDomElement fourth_values = doc.createElement("values");
      fourth_values.setAttribute("x",layer["mx"]);
      fourth_values.setAttribute("y",layer["my"]);
      fourth_values.setAttribute("z",layer["mz"]);	
      third_mag.appendChild(fourth_values);
      QDomElement fourth_angle = doc.createElement("angle");
      fourth_angle.setAttribute("initial",layer["initialAngle"]);
      fourth_angle.setAttribute("final",layer["finalAngle"]);
      fourth_angle.setAttribute("step",layer["angleStep"]);
      third_mag.appendChild(fourth_angle);
      QDomElement third_Demag = doc.createElement("demagnetizationTensor");
      second_Layer.appendChild(third_Demag);
      QDomElement fourth_self = doc.createElement("self");
      fourth_self.setAttribute("x",layer["ndx"]);
      fourth_self.setAttribute("y",layer["ndy"]);
      fourth_self.setAttribute("z",layer["ndz"]);
      third_Demag.appendChild(fourth_self);
      QDomElement fourth_mutual = doc.createElement("mutual");
      fourth_mutual.setAttribute("x",layer["nrx"]);
      fourth_mutual.setAttribute("y",layer["nry"]);
      fourth_mutual.setAttribute("z",layer["nrz"]);
      third_Demag.appendChild(fourth_mutual);	 
    }
 
  QFile file(fileName);
  QTextStream stream(&file);
  file.open(QIODevice::WriteOnly);
  stream << doc.toString();
  file.close();
}
